<?php
/**
 * @title 423. 从英文中重建数字 - 统计
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $s
     * @return String
     */
    function originalDigits($s) {
        /**
         * @since 2021/11/24 字符数组
         */
        $charArray = [
            "e" => 0,
            "g" => 0,
            "f" => 0,
            "i" => 0,
            "h" => 0,
            "o" => 0,
            "n" => 0,
            "s" => 0,
            "r" => 0,
            "u" => 0,
            "t" => 0,
            "w" => 0,
            "v" => 0,
            "x" => 0,
            "z" => 0,
        ];

        /**
         * @since 2021/11/24 统计字符数量
         */
        for($i=0; $i<strlen($s); $i++){
            $charArray[$s[$i]] += 1;
        }

        /**
         * @since 2021/11/24 数字数组
         */
        $numArray = array_fill(0, 10, 0);

        /**
         * @since 2021/11/24 第1轮，找e-eight, u-four, w-two, x-six, z-zero
         * @since 2021/11/24 第2轮，找f-five, r-three
         * @since 2021/11/24 第3轮，v-seven
         * @since 2021/11/24 第4轮，i-nine
         * @since 2021/11/24 第5轮，o-one
         */
        $dataArray = [
            [8, "g", "eight"],
            [4, "u", "four"],
            [2, "w", "two"],
            [6, "x", "six"],
            [0, "z", "zero"],
            [5, "f", "five"],
            [3, "r", "three"],
            [7, "v", "seven"],
            [9, "i", "nine"],
            [1, "o", "one"],
        ];

        /**
         * @since 2021/11/24 查找数字
         */
        foreach ($dataArray as list($num, $char, $word)){
            if($charArray[$char] > 0){
                /**
                 * @since 2021/11/24 统计num的数量
                 */
                $cnt = $charArray[$char];
                $numArray[$num] = $cnt;

                /**
                 * @since 2021/11/24 扣去数量
                 */
                for($i=0; $i<strlen($word); $i++){
                    $charArray[$word[$i]] -= $cnt;
                }
            } else {}
        }

        /**
         * @since 2021/11/24 返回字符串
         */
        $result = "";
        foreach ($numArray as $num=>$cnt){
            if($cnt > 0){
                $result .= str_repeat($num, $cnt);
            } else {}
        }

        /**
         * @return
         */
        return $result;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202111/2021.11.24.php
 */
$datas = [
    ["owoztneoer"], // 012
    ["fviefuro"], // 45
    ["oneone"], // 11
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->originalDigits(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
